var navtab;
layui.use(['elements','layer','larry','navtab','form'], function() {
    var layer = layui.layer,
        larry=layui.larry(),
        navtab = layui.navtab({
            elem: '#larry-tab'
        }),
        $ = layui.jquery,
        form = layui.form;
    var initFunc = function() {
        // 顶部左侧导航控制开关
        $('#toggle').click(function () {
            $("#template-layout").toggleClass("hideLeftNav").toggleClass("showLeftNav");
            // if(!$('#template-layout').hasClass('larry-mobile')){
            //     menuTabOperate.leftMenuFunc();
            // }
            
        });
        $(".layui-tab-item iframe").height($(document).height() - 148);
        $(window).resize(function() {
            $(".layui-tab-item iframe").height($(document).height() - 148);
        });
        //清除缓存
        $('#clearCached').on('click', function () {
            larry.cleanCached();
            layer.alert('缓存清除完成!本地存储数据也清理成功！', {icon: 1, title: '系统提示'}, function () {
                location.reload();//刷新
            });
        });

        // 设置主题
        var fScreen = localStorage.getItem("fullscreen_info");
        var themeName = localStorage.getItem('themeName');
        if (themeName) {
            $("body").attr("class", "");
            $("body").addClass("larryTheme-" + themeName);
        }
        if (fScreen && fScreen != 'false') {
            var fScreenIndex = layer.alert('按ESC退出全屏', {
                title: '进入全屏提示信息',
                skin: 'layui-layer-lan',
                closeBtn: 0,
                anim: 4,
                offset: '100px'
            }, function () {
                entryFullScreen();
                $('#FullScreen').html('<i class="larry-icon larry-quanping"></i>退出全屏');
                layer.close(fScreenIndex);
            });
        }
        $('#larryTheme').on('click', function () {
            var fScreen = localStorage.getItem('fullscreen_info');
            var themeName = localStorage.getItem('themeName');
            layer.open({
                type: 1,
                title: false,
                closeBtn: true,
                shadeClose: false,
                shade: 0.35,
                area: ['450px', '300px'],
                isOutAnim: true,
                resize: false,
                anim: Math.ceil(Math.random() * 6),
                content: $('#LarryThemeSet').html(),
                success: function (layero, index) {
                    if (fScreen && fScreen != 'false') {
                        $("input[lay-filter='fullscreen']").attr("checked", "checked");
                    }
                    if (themeName) {
                        $("#themeName option[value='" + themeName + "']").attr("selected", "selected");
                    }
                    form.render();
                }
            });

            // 全屏开启
            form.on('switch(fullscreen)', function (data) {
                var fValue = data.elem.checked;
                localStorage.setItem('fullscreen_info', fValue); //fullscreen_info:fValue

            });
            // 主题设置
            form.on('select(larryTheme)', function (data) {
                var themeValue = data.value;
                localStorage.setItem('themeName', themeValue);//themeName:themeValue
                if (themeName) {
                    $("body").attr("class", "");
                    $("body").addClass("larryTheme-" + themeName);
                }
                form.render('select');
            });

            // form.on('submit(submitlocal)',function(data){

            // })
        });
        // 全屏切换
        $('#FullScreen').bind('click', function () {
            var fullscreenElement =
                document.fullscreenElement ||
                document.mozFullScreenElement ||
                document.webkitFullscreenElement;
            if (fullscreenElement == null) {
                entryFullScreen();
                $(this).html('<i class="larry-icon larry-quanping"></i>退出全屏');
            } else {
                exitFullScreen();
                $(this).html('<i class="larry-icon larry-quanping"></i>全屏');
            }
        });

        // 进入全屏：
        function entryFullScreen() {
            var docE = document.documentElement;
            if (docE.requestFullScreen) {
                docE.requestFullScreen();
            } else if (docE.mozRequestFullScreen) {
                docE.mozRequestFullScreen();
            } else if (docE.webkitRequestFullScreen) {
                docE.webkitRequestFullScreen();
            }
        }

        // 退出全屏
        function exitFullScreen() {
            var docE = document;
            if (docE.exitFullscreen) {
                docE.exitFullscreen();
            } else if (docE.mozCancelFullScreen) {
                docE.mozCancelFullScreen();
            } else if (docE.webkitCancelFullScreen) {
                docE.webkitCancelFullScreen();
            }
        }

        // 锁屏控制
        $('#lock').mouseover(function () {
            layer.tips('请按Alt+L快速锁屏！', '#lock', {
                tips: [1, '#FF5722'],
                time: 2000
            });
        });
        // 快捷键锁屏设置
        $(document).keydown(function (e) {
            if (e.altKey && e.which == 76) {
                lockSystem();
            }
        });
        checkLockStatus('0');
        // 锁定屏幕
        function lockSystem() {
            /*var url = '../backstage/datas/lock.json';
            $.get(
                url,
                function (data) {
                    if (data == '1') {
                        checkLockStatus(1);
                    } else {
                        layer.alert('锁屏失败，请稍后再试！');
                    }
                });*/
                checkLockStatus(1);
            startTimer();
        }

        //解锁屏幕
        function unlockSystem() {
            // 与后台交互代码已移除，根据需求定义或删除此功能

            checkLockStatus(0);
        }

        // 点击锁屏
        $('#lock').click(function () {
            lockSystem();
        });
        // 解锁进入系统
        $('#unlock').click(function () {
            unlockSystem();
        });
        // 监控lock_password 键盘事件
        $('#lock_password').keypress(function (e) {
            var key = e.which;
            if (key == 13) {
                unlockSystem();
            }
        });

        function startTimer() {
            var today = new Date();
            var h = today.getHours();
            var m = today.getMinutes();
            var s = today.getSeconds();
            m = m < 10 ? '0' + m : m;
            s = s < 10 ? '0' + s : s;
            $('#time').html(h + ":" + m + ":" + s);
            t = setTimeout(function () {
                startTimer()
            }, 500);
        }

        // 锁屏状态检测
        function checkLockStatus(locked) {
            // 锁屏
            if (locked == 1) {
                $('.lock-screen').show();
                $('#locker').show();
                $('#layui_layout').hide();
                $('#lock_password').val('');
            } else {
                $('.lock-screen').hide();
                $('#locker').hide();
                $('#layui_layout').show();
            }
        }
    };
    var menuTabOperate = {
        menuInit: function() {
            $.ajaxSettings.async = false;
            //头部init
            $.getJSON('src/js/data/top_menu.json?t=' + new Date(), function (result) {
                larry.set({
                    elem: '#template-top-menu',
                    data: result,
                    cached: false
                });
                larry.render();
            });
            //左侧导航init
            var $menu = $('#template-top-menu');
            $menu.find('li.layui-nav-item').each(function() {
                var $that = $(this);
                //绑定一级导航的点击事件
                $that.on('click', function() {
                    var id = $that.data('pid');
                    menuTabOperate.leftMenuFunc(true);
                    $.getJSON('src/js/data/pid_' + id + '.json?t=' + new Date(), function (result) {
                        larry.set({
                            elem: '#template-left-menu',
                            data: result,
                            spreadOne: true
                        });
                        larry.render();
                        //监听左侧导航点击事件
                        larry.on('click(leftMenu)', function (data) {
                            navtab.tabAdd(data.field);
                        });
                    });
                });

            });
            // 左侧导航点击事件
            $menu.find('li[data-pid=0]').click();
            $("#template-left-menu").find("li").eq(0).addClass('layui-this');
            $.ajaxSettings.async = true;        
            this.contentFunc();
        },
        contentFunc: function() {
            $('#buttonRCtrl').find('dd').each(function () {
                $(this).on('click', function () {
                    var eName = $(this).children('a').attr('data-eName');
                    navtab.tabCtrl(eName);
                });
            });
            // 刷新iframe
            $("#refresh_iframe").click(function () {
                $(".layui-tab-content .layui-tab-item").each(function () {
                    if ($(this).hasClass('layui-show')) {
                        $(this).children('iframe')[0].contentWindow.location.reload(true);
                    }
                });
            });
        },
        leftMenuFunc: function(isOpen) {
            var sideWidth = $('#template-side').width();
            var bodyW = $('#template-body').width();
            if(isOpen){
                return; 
            }           
            if (sideWidth === 200) {
                bodyW = 200-56;
                $('#template-body').animate({
                    left: '56px',
                    width: bodyW
                });
                $('#template-footer').animate({
                    left: '56px',
                    width: bodyW
                });
                $('#template-side').animate({
                    width: '56px'
                });
            } else {
                bodyW -= 200-56;
                $('#template-body').animate({
                    left: '200px',
                    width: bodyW
                });
                $('#template-footer').animate({
                    left: '200px',
                    width: bodyW
                });
                $('#template-side').animate({
                    width: '200px'
                });
            }
        }
    };
    menuTabOperate.menuInit();
    initFunc();

});